<?php
namespace app\admin\logic;

use app\common\logic\Base;
use think\Db;
use think\facade\App;

class Domain extends Base {
    
    /**
     * @desc 订单列表
     * @param array $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($params) {
        $where = [];
        if($params['domainName']) {
            $where[] = ['domain_name', 'like', '%'.$params['domainName'].'%'];
        }
    
        if(isset($params['status']) && $params['status'] !== '') {
            $where[] = ['domain_status', '=', $params['status']];
        }
    
        if($params['userName']) {
            $userId = App::model('User')
                ->where('user_phone', 'like', '%'.$params['userName'].'%')
                ->whereOr('user_email', 'like', '%'.$params['userName'].'%')
                ->value('user_id');
            if($userId) {
                $where[] = ['domain_user_id', '=', $userId];
            }
        }
        
        $list = App::model('Domain')
            ->where($where)
            ->order('domain_id', 'desc')
            ->paginate($params['pageSize']);
    
        $pagination = $list->render();
        if($list) {
            $list = $list->toArray();
            $list = output_format($list['data'], 'domain');
            
            $userIds = array_unique(array_column($list, 'userId'));
            $userInfo = App::model('User')
                ->where('user_id', 'in', $userIds)
                ->column('user_phone,user_email', 'user_id');
            foreach ($list as $k => $v) {
                $list[$k]['userName'] = $userInfo[$v['userId']]['user_phone'] ?: $userInfo[$v['userId']]['user_email'];
            }
        }
        return [
            'list'         => $list,
            'params'       => $params,
            'pagination'   => $pagination
        ];
    }
    
    /**
     * @desc 确认处理
     * @param array $params
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function confirm($params) {
        $domainInfo = App::model('Domain')
                    ->where('domain_id', '=', $params['id'])
                    ->find();
        
        //判断订单是否存在
        if(!$domainInfo) {
            $this->errorCode = EC_AD_DOMAIN_INFO_NOT_EXIST;
            return false;
        }
        
        if($domainInfo['domain_status'] == DOMAIN_STATUS_YES) {
            $this->errorCode = EC_AD_DOMAIN_INFO_ALREADY_PROCESSED;
            return false;
        }
        
        $result = App::model('Domain')
                    ->where('domain_id', '=', $params['id'])
                    ->update([
                        'domain_member_id'    => $params['memberId'],
                        'domain_status'       => DOMAIN_STATUS_YES,
                        'domain_modify_time'  => date('Y-m-d H:i:s'),
                        'domain_confirm_time' => date('Y-m-d H:i:s'),
                    ]);
    
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
    
    /**
     * @desc 应用详情
     * @param array $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($params) {
        $domainInfo = App::model('Domain')
            ->where('domain_id', '=', $params['id'])
            ->find();
        return [
            'info'         => output_format($domainInfo, 'domain')
        ];
    }
}